﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SavannaFrame.Classes;

namespace SavannaFrame
{
    public partial class SlotFrm : Form
    {
        MarkerEditFull markerEditFull;

        public SlotFrm()
        {
            InitializeComponent();
            cmbType.DataSource = ClassFactory.kBase.SlotTypes();
            cmbInherit.DataSource = ClassFactory.kBase.InheritType();
        }

        Slot slot;

        public SlotFrm(Slot slot)
        {
            // инициализация типов списков
            
            InitializeComponent();
            this.slot = slot;
            txtName.Text = slot.SlotName;
            // заполнение cmb списком вершин
            cmbType.DataSource = ClassFactory.kBase.SlotTypes();
            cmbType.SelectedIndex = cmbType.Items.IndexOf(slot.SlotType.ToString());
            if (slot.IsSystem)
            {
                chbSystem.Checked = true;
                chbSystem.Enabled = false;
                chbInherit.Enabled = false;
            }
            else
            {
                chbSystem.Checked = false;
                chbSystem.Enabled = false;
                chbInherit.Enabled = true;
            }
            cmbInherit.DataSource = ClassFactory.kBase.InheritType();
            cmbInherit.SelectedIndex = cmbInherit.Items.IndexOf(slot.SlotInheritance.ToString());
            //cmbValue.DataSource = ClassFactory.kBase.DomainList();

            if (slot.SlotType == SlotType.FramePrototype || slot.SlotType == SlotType.Frame)
            {
                cmbType_SelectedIndexChanged(null, null);
                List<string> frameNames = ClassFactory.kBase.strFrmList();
                int n=frameNames.Count;
                for (int i = 0; i < n; ++i)
                    frameNames[i] = frameNames[i].Trim().ToLower();
                
                int ind = frameNames.IndexOf(KnowLedgeBase.getFrameByID(slot.frameId).FrameNameTrimmed);
                if (ind > -1)
                    cmbValue.SelectedIndex = ind;
            }


            // заполнение списка типов наследования
        }



        private void label4_Click(object sender, EventArgs e)
        {

        }

        private bool validateData()
        {
            bool result = true;
            if (txtName.TextLength == 0 || txtName.Text.Contains('#') || cmbType.SelectedIndex == -1)
                result = false;
            return result;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.validateData())
            {
                slot.SlotMarker = markerEditFull.GetMarker();
                slot.SlotName = txtName.Text.ToString().Trim();
                switch (cmbType.SelectedIndex)
                {
                        
                    case 0:
                        slot.SlotType = SlotType.Boolean;
                        break;
                    case 1:
                        slot.SlotType = SlotType.Frame;
                        break;
                    case 2:
                        slot.SlotType = SlotType.FramePrototype;
                        break;
                    case 3:
                        slot.SlotType = SlotType.Integer;
                        break;
                    case 4:
                        slot.SlotType = SlotType.Procedure;
                        break;
                    case 5:
                        slot.SlotType = SlotType.Production;
                        break;
                    case 6:
                        slot.SlotType = SlotType.String;
                        break;
                }
                //if (cmbDefValue.SelectedItem != null)
                //    slot.SlotDefault = cmbDefValue.SelectedItem.ToString();
                if (cmbValue.SelectedItem == null)
                    slot.SlotDefault = "";
                else slot.SlotDefault = cmbValue.SelectedItem.ToString();
                if (slot.SlotType == SlotType.Frame || slot.SlotType == SlotType.FramePrototype)
                    slot.frameId = KnowLedgeBase.getFrameByName(cmbValue.SelectedItem.ToString()).FrameId;

                switch (cmbType.SelectedIndex)
                {
                    case 0:
                        slot.SlotInheritance = SlotInherit.Override;
                        break;
                    case 1:
                        slot.SlotInheritance = SlotInherit.Range;
                        break;
                    case 2:
                        slot.SlotInheritance = SlotInherit.Same;
                        break;
                    case 3:
                        slot.SlotInheritance = SlotInherit.Unique;
                        break;
                }
                ClassFactory.kBase.AddSlot(slot, slot.ParentId);
                this.Close();
            }
            else
                MessageBox.Show("Не все данные введены корректно.");
        }

        //private void cmbValue_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    cmbDefValue.DataSource = ClassFactory.kBase.DomainValues(cmbValue.SelectedItem.ToString());
        //}

        private void SlotFrm_Load(object sender, EventArgs e)
        {
            markerEditFull = new MarkerEditFull(slot);

            markerEditFull.Dock = DockStyle.Fill;
            panelMarker.Controls.Add(markerEditFull);
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbType.SelectedIndex == 1 || cmbType.SelectedIndex == 2)
                cmbValue.DataSource = ClassFactory.kBase.strFrmList();
            else
            {
                
            }
        }
    }
}

